package com.fckbug.service.impl;

import java.util.List;

import com.fckbug.dao.UserDAO;
import com.fckbug.service.UserService;
import com.fckbug.util.JiaMi;
import com.fckbug.vos.Acc;

public class UserServiceImpl implements UserService {
	private UserDAO userDAO;

	public UserDAO getUserDAO() {
		return userDAO;
	}

	public void setUserDAO(UserDAO userDAO) {
		this.userDAO = userDAO;
	}

	@Override
	public List getUserByUsername(String username) {
		return userDAO.getUserByUsername(username);
	}

	@Override
	public boolean saveRegUser(String username, String pwd, String nickname) throws Exception {
		String md5 = JiaMi.md5JiaMi(pwd);
		Integer result = userDAO.saveRegUser(username, md5, nickname);
		if( result != null && result == 1) {
			return true;
		}
		return false;
	}

	@Override
	public Acc getUserByUserId(Integer userId) {
		List list = userDAO.getUserByUserId(userId);
		Acc acc = new Acc();
		if(list.size()>0) {
			Object[] objs = (Object[])list.get(0);
			acc.setId((Integer)objs[0]);
			acc.setUserName((String)objs[1]);
			acc.setNickName((String)objs[2]);
			acc.setEmail((String)objs[3]);
			acc.setRealName(objs[4]==null?"":(String)objs[4]);
			acc.setLocation(objs[5]==null?"":(String)objs[5]);
			acc.setBirthday(objs[6]==null?"":(String)objs[6]);
			acc.setCompany(objs[7]==null?"":(String)objs[7]);
		}
		return acc;
	}

	@Override
	public boolean updateUser(Acc acc) {
		int result = userDAO.updateUser(acc);
		if(result > 0) {
			return true;
		}
		return false;
	}

	@Override
	public List listTopUsers() {
		return userDAO.listTopUsers();
	}
}
